package com.wbao.dianniu.ui;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.mobileim.fundamental.widget.refreshlist.PullToRefreshBase;
import com.alibaba.mobileim.fundamental.widget.refreshlist.PullToRefreshListView;
import com.wbao.dianniu.R;
import com.wbao.dianniu.adapter.KnowListAdapter;
import com.wbao.dianniu.data.KnowledgeData;
import com.wbao.dianniu.listener.IKnowledgeListListener;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.manager.KnowListManager;
import com.wbao.dianniu.utils.ActivityUtils;
import com.wbao.dianniu.utils.Notification;

import java.util.ArrayList;
import java.util.List;

/**
 * 我收藏的干货
 * <p>
 * Created by yejianjun on 2018/11/10.
 */
public class CollectKnowFragment extends Fragment implements IKnowledgeListListener, View.OnClickListener /*, KnowledgeHelper.IKnowledgeChangedListener */ {

    private PullToRefreshListView pullListView;
    private ListView listView;
    private final int PAGE_COUNTS = 10;
    private int currentPage = 0;
    private KnowListManager manager;
    private KnowListAdapter adapter;
    private final int TYPE = 3;
    private TextView noDataTV;
    //是否是下拉刷新
    private boolean isPullDown = true;

    public static CollectKnowFragment instance(Context context) {
        CollectKnowFragment view = new CollectKnowFragment();
        return view;
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_my_knowledge, null);
        initView(view);
        initData();
        requestData(currentPage);
        return view;
    }

    private void initView(View view) {
        pullListView = view.findViewById(R.id.my_knowledge_listview);
        pullListView.setMode(PullToRefreshBase.Mode.BOTH);
        pullListView.setOnRefreshListener(onRefreshListener);
        listView = pullListView.getRefreshableView();
        registerForContextMenu(listView);

        noDataTV = view.findViewById(R.id.no_data_tv);
    }

    private void initData() {
        manager = new KnowListManager(getContext());
        manager.addKnowledgeListListener(this);
        adapter = new KnowListAdapter(getContext(), 1);
        adapter.addData(new ArrayList<KnowledgeData>());
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(itemClickListener);

//        KnowledgeHelper.getInstance().addListener(this);
    }

    AdapterView.OnItemClickListener itemClickListener = new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            // TODO Auto-generated method stub
            KnowledgeData data = (KnowledgeData) adapter.getItem(position - 1);
            ActivityUtils.intoKnowDetailActivity(getContext(), 0, data);
        }
    };

    PullToRefreshBase.OnRefreshListener2 onRefreshListener = new PullToRefreshBase.OnRefreshListener2() {
        // 下拉Pulling Down
        @Override
        public void onPullDownToRefresh() {
            //请求最新的数据
            isPullDown = true;
            currentPage = 0;
            pullUpRequest();
        }

        @Override
        public void onPullUpToRefresh() {
            //请求下一页数据
            isPullDown = false;
            currentPage++;
            pullUpRequest();
        }
    };


    // 上拉刷新数据请求
    private void pullUpRequest() {
        //没有搜索结果 或者数据为空 上拉没有效果
        requestData(currentPage);
    }

    private void requestData(int page) {
        manager.knowledgeList(GlobalContext.getAccountId(), TYPE, null, page * PAGE_COUNTS, PAGE_COUNTS);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            default:
                break;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (null != manager)
            manager.removeKnowledgeListListener(this);

//        KnowledgeHelper.getInstance().removeListener(this);
    }

    @Override
    public void onKnowledgeListFailure(int errorCode, String errorMsg) {
        pullListView.onRefreshComplete(false, false);
        Notification.toast(getActivity(), errorMsg == null ? "" : errorMsg);
    }

    @Override
    public void onKnowledgeListSuccess(List<KnowledgeData> list) {
        pullListView.onRefreshComplete(false, false);
        if (null != list) {
            if (isPullDown) {
                adapter.cleanData();
            }
            adapter.addData(list);
            if (adapter.getCount() == 0) {
                noDataTV.setVisibility(View.VISIBLE);
            } else {
                noDataTV.setVisibility(View.GONE);
            }
            adapter.notifyDataSetChanged();
        }
    }


}
